Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging

Imports Phoenix.DataAccess
Imports Phoenix.Events
Imports Phoenix.Forms


Namespace Helpers

    Public Class EmployeeHelper : Inherits BaseHelper

#Region " Edit Commands "
        Public Shared Function Copy(ByVal obj As DataSetData.Employee) As DataSetData.Employee
            Dim s As DataSetData.Employee = AppDataLayer.Employees.NewEmployee
            s = Copy(s, obj)
            s.FullName = "Copy of " & s.FullName

            Return s
        End Function

        Private Shared Function Copy(ByVal s As DataSetData.Employee, ByVal obj As DataSetData.Employee) As DataSetData.Employee
            s.FullName = obj.FullName
            s.Address = obj.Address
            s.IsAttendant = obj.IsAttendant
            s.IsOperator = obj.IsOperator
            s.MasterLevelID = obj.MasterLevelID
            s.Password = obj.Password
            s.Position = obj.Position
            s.SecurityLevel = obj.SecurityLevel
            s.Sys = False
            s.Phone = obj.Phone

            Return s
        End Function

        Public Shared Sub DeleteEmployee(ByVal obj As DataSetData.Employee, ByVal sender As Object)
            DeleteEmployee(obj, sender, True)
        End Sub

        Public Shared Sub DeleteEmployee(ByVal obj As DataSetData.Employee, ByVal sender As Object, ByVal confirm As Boolean)
            If obj.IsAttendant Then
                Dim ds As DataSetAppointments = AppDataLayer.GetEmployeePendingAppointments(obj.EmployeeID)
                If ds.Appointments.Rows.Count <> 0 Then
                    AppMessageBox(String.Format(Res.ErrorEmployeeHasBookedAppointments, obj.FullName), MsgBoxStyle.Critical)
                    Return
                End If
            End If

            If CloseForm(obj) Then
                If confirm Then
                    If Not AppConfig.SilenceEmployeeDeletion Then
                        Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.EmployeeDeleteQuestion, obj.FullName), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                        If result <> MsgBoxResult.Yes Then Return
                    End If
                End If

                AppRaiseEvent(sender, New PhoenixEventArgs(obj, PhoenixEventCode.BizRemoved))
                Try
                    DeleteEmployeeCore(obj)
                Catch
                    AppRaiseEvent(sender, New PhoenixEventArgs(obj, PhoenixEventCode.BizAdded))
                End Try
            End If
        End Sub

        Public Shared Sub DeleteEmployees(ByVal list As IList, ByVal sender As Object)
            If list.Count = 1 Then
                DeleteEmployee(list(0), sender, True)
            Else
                DeleteEmployees(list, sender, True)
            End If
        End Sub

        Public Shared Sub DeleteEmployees(ByVal list As IList, ByVal sender As Object, ByVal confirm As Boolean)
            If (Not AppConfig.SilenceEmployeesDeletion) And confirm Then
                Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.EmployeesDeleteQuestion, list.Count), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                If result <> MsgBoxResult.Yes Then Return
            End If

            OperationCanceled = False
            Dim mylist(list.Count - 1) As DataSetData.Employee
            list.CopyTo(mylist, 0)
            For i As Long = 0 To list.Count - 1
                DeleteEmployee(mylist(i), sender, False)
                If OperationCanceled Then Return
            Next
        End Sub

        Public Shared Sub UpdateEmployee(ByVal employee As DataSetData.Employee, ByVal sender As Object)
            Dim e As PhoenixEventArgs

            If employee.RowState = DataRowState.Detached Then
                e = New PhoenixEventArgs(employee, PhoenixEventCode.BizAdded)
            Else
                e = New PhoenixEventArgs(employee, PhoenixEventCode.BizUpdated)
                If Not employee.IsAttendant Then
                    Dim ds As DataSetAppointments = AppDataLayer.GetEmployeePendingAppointments(employee.EmployeeID)
                    If ds.Appointments.Rows.Count <> 0 Then
                        AppMessageBox(String.Format(Res.ErrorEmployeeHasBookedAppointments, employee.FullName), MsgBoxStyle.Critical)
                        Throw New CancelOperationException
                    End If
                End If
            End If



            UpdateEmployeeCore(employee)

            AppRaiseEvent(sender, e)
        End Sub


        Public Shared Sub CopyEmployeeSchedule(ByVal eDest As DataSetData.Employee, ByVal eSource As DataSetData.Employee, ByVal sender As Object)
            For Each et As DataSetData.EmployeeTime In eSource.GetEmployeeTimes
                Dim net As DataSetData.EmployeeTime = AppDataLayer.EmployeeTimes.NewEmployeeTime
                net.Employee = eDest
                net.StartTime = et.StartTime
                net.FinishTime = et.FinishTime
                net.DayOfWeek = et.DayOfWeek
                AppDataLayer.EmployeeTimes.AddEmployeeTime(net)
            Next
            AppDataLayer.UpdateEmployeeTimes(AppDataLayer.EmployeeTimes)

            Dim btDS As DataSetAppointments = AppDataLayer.GetBusyTimes(eSource.EmployeeID)
            btDS.EnforceConstraints = False
            For Each bt As DataSetAppointments.BusyTime In btDS.BusyTimes.Select
                Dim nbt As DataSetAppointments.BusyTime = btDS.BusyTimes.NewBusyTime
                nbt.Descript = bt.Descript
                nbt.EmployeeID = eDest.EmployeeID
                nbt.ExactDate = bt.ExactDate
                nbt.FinishTime = bt.FinishTime
                nbt.StartTime = bt.StartTime
                nbt.Busy = bt.Busy
                btDS.BusyTimes.AddBusyTime(nbt)
            Next
            AppDataLayer.UpdateBusyTimes(btDS.BusyTimes)
            AppRaiseEvent(sender, New PhoenixEventArgs(eDest, PhoenixEventCode.ScheduleChanged))
        End Sub

#End Region

#Region " CRUD Methods "
        Private Shared Sub UpdateEmployeeCore(ByVal employee As DataSetData.Employee)
            If employee.RowState = DataRowState.Detached Then
                AppDataLayer.Employees.AddEmployee(employee)
            End If
            AppDataLayer.UpdateEmployee(employee)
        End Sub

        Private Shared Sub DeleteEmployeeCore(ByVal employee As DataSetData.Employee)
            Dim ets() As DataSetData.EmployeeTime = employee.GetEmployeeTimes()

            employee.Delete()
            AppDataLayer.UpdateEmployeeTimes(ets)
            AppDataLayer.UpdateEmployee(employee)
        End Sub
#End Region

#Region " Others Helpers "
        Public Shared Function GetDefaultImage() As Image
            Return Res.EmployeeNoPhoto
        End Function

        Public Shared Function GetEmployeeImage(ByVal Employee As DataSetData.Employee) As Image
            If Employee.IsPhotoNull Then
                Return GetDefaultImage()
            Else
                Return Image.FromStream(New MemoryStream(Employee.Photo))
            End If
        End Function

        Public Shared Sub SetEmployeeImage(ByVal Employee As DataSetData.Employee, ByVal img As Image)
            If Not img Is Nothing Then
                Dim ms As MemoryStream = New MemoryStream
                img.Save(ms, ImageFormat.Jpeg)
                Employee.Photo = ms.ToArray()
            Else
                Employee.SetPhotoNull()
            End If
        End Sub

#End Region

    End Class
End Namespace
